// pages/my-pages/edit-item/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseItem: {},
    showStaffValue: '',
    user: {},
    addForm: {},
    id: -1
  },
  //点击了店员列表
  checkStaffButton: function (e) {
    console.log('点击了店员列表')
    wx.navigateTo({
      url: '/pages/my-pages/select-staff/index',
    })
  },
  //点击保存
  confirmSaveButton: function (e) {
    let that = this;
    let obj;
    console.log(that.data.user)
    if (-1 == that.data.id) {
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'basicItem/addBasicItem',
        data: that.data.baseItem
      }
    } else {
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'basicItem/updateBasicItem',
        data: that.data.baseItem
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        ui.showToast('保存成功')
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //项目名称变更
  inputBaseName: function (e) {
    this.setData({
      'baseItem.baseName': e.detail.value
    })
  },
  //顺序编号变更
  inputSortNumber: function (e) {
    let sortNumber = e.detail.value;
    sortNumber = sortNumber.replace(/\D/g, '')
    this.setData({
      'baseItem.sortNumber': sortNumber
    })
  },
  //写入显示店员文案
  setShowStaffValue: function () {
    let that = this;
    let user = that.data.user;
    if (user != null) {
      if (user.id != null && user.id != undefined) {
        that.setData({
          'baseItem.opeUserId': user.id,
          showStaffValue: user.nickName + ' (' + user.staffPositionText + ')'
        })
      }
    }
    if (that.data.showStaffValue.length > 11) {
      that.setData({
        showStaffValue: that.data.showStaffValue.slice(0, 11)
      })
    }
  },
  //查询基础项目信息
  getBaseItemFunc: function () {
    let that = this;
    let id = that.data.id;
    if (-1 == id || null == id) {
      console.log("新增基础项目");
    } else {
      //获取基础项目信息
      let obj = {
        method: 'GET',
        showLoading: false,
        url: 'basicItem/getBaseItemById?id=' + id
      }
      httpUtils.request(obj).then(res => {
        console.log(res.data.message)
        if (res.data.retCode == 0) {
          this.setData({
            baseItem: res.data.data,
          })
          if(null != res.data.data.opeUser){
            this.setData({
              user: res.data.data.opeUser
            })
          }
          this.setShowStaffValue();
        } else {
          ui.showToast(res.data.message)
        }
      }).catch(err => {
        ui.showToast('请求失败')
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      item: {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setShowStaffValue();
    this.getBaseItemFunc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})